﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.IO;
using HtmlAgilityPack;

namespace DouBanTop250.Model
{
    /// <summary>
    /// 电影
    /// </summary>
    public class MovieModel
    {
        public string Link { get; set; }
        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImageUrl { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 导演信息
        /// </summary>
        public string DoctorInfo { get; set; }
        /// <summary>
        /// 类型信息
        /// </summary>
        public string TypeInfo { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string DespInfo { get; set; }
        /// <summary>
        /// 评价人数
        /// </summary>
        public int PersonNum { get; set; }
        /// <summary>
        /// 评分
        /// </summary>
        public float Rate { get; set; }

        public bool ShowShadow { get; set; }
        /// <summary>
        /// 序号
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 模型转换函数
        /// </summary>
        /// <param name="strRespMessage"></param>
        /// <returns></returns>
        public static List<MovieModel> ParseModel(string strRespMessage)
        {
            if (string.IsNullOrWhiteSpace(strRespMessage))
            {
                return null;
            }

            //解析出所有的div class = 'item'
            //string strParseRule = "<div class=\"item\">[\\s\\S]*?</div>";
            //Regex reg = new Regex(strParseRule);
            //MatchCollection mc = reg.Matches(strRespMessage);
            //if (mc!=null)
            //{
            //    System.Collections.IEnumerator enu = mc.GetEnumerator();
            //    while (enu.MoveNext() && enu.Current != null)
            //    {
            //        Match match = (Match)(enu.Current);
            //        Console.Write(match.Value + "\r\n");
            //    }
            //}
            //XPathDocument xPathDocument = new XPathDocument(new StringReader(strRespMessage));
            string stxpath = "//div[@class='item']";
            string strFindPeople = "<span>(\\d+)人评价</span>";
            string strFindDesp = "<p class=\"\">([\\s\\S]*?)</p>";
            //string 
            //XPathNavigator nav = xPathDocument.CreateNavigator();
            //XPathNodeIterator nodes = nav.Select(stxpath);
            //while (nodes.MoveNext())
            //{
            //    string strValue = nodes.Current.Value;
            //}
            List<MovieModel> models = new List<MovieModel>();
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(strRespMessage);
            HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes(stxpath);
            if (nodes != null)
            {
                int iCount = nodes.Count;
                for (int i = 0; i < iCount; i++)
                {
                    HtmlNode item = nodes[i];
                    MovieModel model = new MovieModel();
                    string strValue = item.InnerHtml;
                    HtmlNode nodeTitle = item.SelectSingleNode("div[@class='info']/div[@class='hd']/a/span[@class='title']");
                    if (nodeTitle != null)
                    {
                        model.Title = nodeTitle.InnerText;
                    }

                    HtmlNode nodeRate = item.SelectSingleNode("div[@class='info']/div[@class='bd']/div[@class='star']/span[@class='rating_num']");
                    if (nodeTitle != null)
                    {
                        float fRate = 0;
                        float.TryParse(nodeRate.InnerText, out fRate);
                        model.Rate = fRate;
                    }
                    Match match = Regex.Match(strValue, strFindPeople);
                    if (match != null && match.Success)
                    {
                        string strValueNum = match.Groups[1].Value;
                        if (int.TryParse(strValueNum, out int num)) {
                            model.PersonNum = num;
                        }
                    }
                    HtmlNode nodeA = item.SelectSingleNode("div[@class='info']/div[@class='hd']/a");
                    model.Link = nodeA?.GetAttributeValue("href","");

                    HtmlNode nodeImg = item.SelectSingleNode("div[@class='pic']/a/img");
                    model.ImageUrl = nodeImg?.GetAttributeValue("src", "");

                    match = Regex.Match(strValue, strFindDesp);
                    if (match != null && match.Success)
                    {
                        string strValueDinfo = match.Groups[1].Value;
                        string[] arrStr = strValueDinfo?.Trim().Replace("<br>","\n").Split('\n');
                        if (arrStr != null && arrStr.Length > 0)
                        {
                            model.DoctorInfo = arrStr[0].Trim().Replace("&nbsp;"," ");
                                model.TypeInfo = arrStr.Length > 1 ? arrStr[arrStr.Length-1].Trim().Replace("&nbsp;", " ") : "";
                        }
                    }

                    HtmlNode nodeInq = item.SelectSingleNode("div[@class='info']/div[@class='bd']/p/span[@class='inq']");
                    model.DespInfo = nodeInq?.InnerText;

                    models.Add(model);
                }
            }
            //m.g
            return models;
        }
    }
}
